package com.bgs.eat.area.service.impl;

import com.bgs.eat.area.entity.AreaEntity;
import com.bgs.eat.area.entity.Vo.CityAreaVo;
import com.bgs.eat.area.entity.dto.getAllCityAreaDto;
import com.bgs.eat.area.mapper.AreaMapper;
import com.bgs.eat.area.service.AreaService;
import com.bgs.eat.config.CommonResult;
import com.bgs.eat.songMenu.entity.SongMenuEntity;
import com.bgs.eat.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName AreaServiceImpl
 * @Description TODO
 * @Author lzy
 * @Date 2022/10/11 13:57
 **/
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    AreaMapper areaMapper;

    @Autowired
    RedisUtil redisUtil;

    /**
    * @Description 获取省市县的数据
    * @Author lzy
    * @Date 2022/10/11 14:00
    * @Return com.bgs.eat.config.CommonResult
    */
    @Override
    public CommonResult getAreaList() {
        Object areaListMap = redisUtil.get("areaListMap");
        if (areaListMap != null && !areaListMap.equals("null")) {
            return CommonResult.ok(areaListMap);
        } else {
            AreaEntity areaEntity = new AreaEntity();
            HashMap<Object, Object> map = new HashMap<>();
            ArrayList<Object> provinceData = new ArrayList<>();
            ArrayList<Object> areaData = new ArrayList<>();
            ArrayList<Object> countyData = new ArrayList<>();
            areaEntity.setPid(0);
            List<AreaEntity> provinceList = areaMapper.select(areaEntity);
            provinceData.add(provinceList);
            Integer id = provinceList.get(0).getId();
            areaEntity.setPid(id);
            List<AreaEntity> provincePidOneIndexList = areaMapper.select(areaEntity);
            provinceData.add(provincePidOneIndexList);
            areaEntity.setPid(provincePidOneIndexList.get(0).getId());
            List<AreaEntity> areaPidOneIndexList = areaMapper.select(areaEntity);
            provinceData.add(areaPidOneIndexList);
            for (AreaEntity province : provinceList) {
                List<AreaEntity> areaEntities = areaMapper.selectArea(province.getId());
                for (AreaEntity area : areaEntities) {
                    List<AreaEntity> countyEntities = areaMapper.selectArea(area.getId());
                    countyData.add(countyEntities);
                }
                areaData.add(areaEntities);
            }
            map.put("provinceData",provinceData);
            map.put("areaData", areaData);
            map.put("countyData", countyData);
            redisUtil.set("areaListMap", map);
            return CommonResult.ok(map);
        }
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 获取全部省的信息
     * @Date: 2022/10/12 19:00
     * @Return: com.bgs.eat.config.CommonResult
     * @Param: null
     */

    @Override
    public CommonResult getAllProvinces() {
        Example example = new Example(AreaEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pid",0);
        List<AreaEntity> areaEntities = areaMapper.selectByExample(example);
        return CommonResult.ok(areaEntities);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据选择的省的ID查询当前省下的所有市
     * @Date: 2022/10/13 10:27
     * @Return: com.bgs.eat.config.CommonResult
     * @Param: [allCityAreaDto]
     */

    @Override
    public CommonResult getAllCityArea(getAllCityAreaDto allCityAreaDto) {

        Example example = new Example(AreaEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pid",allCityAreaDto.getId());
        List<AreaEntity> areaEntities = areaMapper.selectByExample(example);
        Example exampleArea = new Example(AreaEntity.class);
        Example.Criteria criterias = exampleArea.createCriteria();
        criterias.andEqualTo("pid",areaEntities.get(0).getId());
        List<AreaEntity> areaEntitiess = areaMapper.selectByExample(exampleArea);
        CityAreaVo cityAreaVo = new CityAreaVo();
        cityAreaVo.setCityEntities(areaEntities);
        cityAreaVo.setAreaEntities(areaEntitiess);
        return CommonResult.ok(cityAreaVo);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据市ID获取当前市下的所有县/区
     * @Date: 2022/10/13 11:31
     * @Return: com.bgs.eat.config.CommonResult
     * @Param: [id]
     */

    @Override
    public CommonResult getAllArea(Integer id) {
        Example example = new Example(AreaEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pid",id);
        List<AreaEntity> areaEntities = areaMapper.selectByExample(example);
        List<List<AreaEntity>> list = new ArrayList<>();
        for(int i=0;i<areaEntities.size();i++){
            Example exampleArea = new Example(AreaEntity.class);
            Example.Criteria criterias = exampleArea.createCriteria();
            criterias.andEqualTo("pid",areaEntities.get(i).getId());
            List<AreaEntity> areaEntitiess = areaMapper.selectByExample(exampleArea);
            list.add(areaEntitiess);
        }
        return CommonResult.ok(list);
    }

    /**
     * @Author G胖
     * @Description 查询所有地区信息
     * @Date 2022-10-25 下午 07:52
     * @Return

     */
    @Override
    public List<AreaEntity> getAllAreaDetails() {
        AreaEntity areaEntity = new AreaEntity();
        return areaMapper.select(areaEntity);
    }

    /**
     * @Author gpd
     * @Description 查询用户的详细地址
     * @Date 2022-11-01 下午 03:03
     * @Return com.bgs.eat.config.CommonResult

     */
    @Override
    public AreaEntity getUserAreaAddress(Integer id) {
        AreaEntity areaEntity = new AreaEntity();
        areaEntity.setId(id);
        return areaMapper.selectOne(areaEntity);
    }
}
